﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GNSCore.Helpers;
using GNSCore.Managers;
using GNSCore.Model;
using GNSCore.Repositories;

namespace GNSCore.Managers
{
    /// <summary>
    /// For managing user setting data
    /// </summary>
    public class SettingsManager
    {
        private IRepository<Model.UserSettings> _settingsRep;

        /// <summary>
        /// Default constructor for creating object graph
        /// </summary>
        public SettingsManager()
        {
        }

        /// <summary>
        /// Constr with concrete repository implementation paramenter
        /// </summary>
        /// <param name="_rep"></param>
        public SettingsManager(IRepository<UserSettings> _rep)
        {
            _settingsRep = _rep;
        }

        /// <summary>
        /// Saves user settings to the data base
        /// </summary>
        /// <param name="UseMail">Use mail</param>
        /// <param name="UsePhone">Use phone</param>
        /// <param name="PhoneNum">Phone number</param>
        /// <param name="username">Username</param>
        /// <returns>New settings record id</returns>
        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public UserSettings GetUserSettings(string username)
        {
            return _settingsRep.GetAll<UserSettings>().FirstOrDefault(us => us.User.UserName == username);
        }
        /// <summary>
        /// Saves user settings to the data base
        /// </summary>
        /// <param name="UseMail">Use mail</param>
        /// <param name="UsePhone">Use phone</param>
        /// <param name="PhoneNum">Phone number</param>
        /// <param name="username">Username</param>
        /// <returns>New settings record id</returns>
        public int SaveSettingsForUser(string username, bool UseMail, bool UsePhone, string PhoneNum,
            bool UseTemperature, double temperatureProcent, int temperaturePeriod,
            bool UsePressure, double MaxPressure, double MinPressure, bool UseFogMaxProcent, double FogMaxProcent,
            bool UseHumidityMaxProcent, double HumidityMaxProcent)
        {
            GNSDBEntities ctx = ContextHelper.Instance.Context;
            User currentUser = ctx.User.FirstOrDefault(us => us.UserName == username);

            UserSettings uset = new UserSettings();

            uset.User = currentUser;
            uset.UseEmail = UseMail;
            uset.UseSMS = UsePhone;
            uset.PhoneNumber = PhoneNum;

            uset.UseTemperature = UseTemperature;
            uset.TemperatureProcent = temperatureProcent;
            uset.TemperaturePeriod = temperaturePeriod;

            uset.UsePressure = UsePressure;
            uset.MaxPressure = MaxPressure;
            uset.MinPressure = MinPressure;

            uset.UseFogMaxProcent = UseFogMaxProcent;
            uset.FogMaxProcent = FogMaxProcent;

            uset.UseHumidityMaxProcent = UseHumidityMaxProcent;
            uset.HumidityMaxProcent = HumidityMaxProcent;
            ctx.AddToUserSettings(uset);
            System.Threading.Thread.CurrentThread.CurrentCulture =
                System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            System.Threading.Thread.CurrentThread.CurrentUICulture =
            System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            //ctx.Refresh(System.Data.Objects.RefreshMode.ClientWins,uset);
            ctx.SaveChanges();
            System.Threading.Thread.CurrentThread.CurrentCulture =
            System.Globalization.CultureInfo.CreateSpecificCulture("uk-UA");
            System.Threading.Thread.CurrentThread.CurrentUICulture =
            System.Globalization.CultureInfo.CreateSpecificCulture("uk-UA");
            return uset.Id;

        }
        public int ModifySettingsForUser(string username, bool UseMail, bool UsePhone, string PhoneNum,
           bool UseTemperature, double temperatureProcent, int temperaturePeriod,
           bool UsePressure, double MaxPressure, double MinPressure, bool UseFogMaxProcent, double FogMaxProcent,
           bool UseHumidityMaxProcent, double HumidityMaxProcent)
        {
            //GNSDBEntities ctx = ContextHelper.Instance.Context;
            //User currentUser = ctx.User.FirstOrDefault(us => us.UserName == username);

            UserSettings userset =
                _settingsRep.GetAll<UserSettings>().
                FirstOrDefault(us => us.User.UserName == username);


            userset.UseEmail = UseMail;
            userset.UseSMS = UsePhone;
            userset.PhoneNumber = PhoneNum;

            userset.UseTemperature = UseTemperature;
            userset.TemperatureProcent = temperatureProcent;
            userset.TemperaturePeriod = temperaturePeriod;

            userset.UsePressure = UsePressure;
            userset.MaxPressure = MaxPressure;
            userset.MinPressure = MinPressure;

            userset.UseFogMaxProcent = UseFogMaxProcent;
            userset.FogMaxProcent = FogMaxProcent;

            userset.UseHumidityMaxProcent = UseHumidityMaxProcent;
            userset.HumidityMaxProcent = HumidityMaxProcent;

            _settingsRep.SaveChanges(true);
            return userset.Id;

        }

    }
}